import serial

class uart():
    def __init__(self, port, baudrate=115200):
        self.port = port
        self.baudrate = baudrate

        self.ser = serial.Serial(self.port, self.baudrate)

    def send(self, num1=0, num2=90):
        if not (-45 <= num1 <= 45):
            raise ValueError("数字1必须在 -45 到 45 的范围内")
        if not (45 <= num2 <= 135):
            raise ValueError("数字2必须在 45 到 135 的范围内")
        
        packet = f"!d:{num1:.2f},{num2:.2f}\r\n".encode('utf-8')
        # 发送数据包
        self.ser.write(packet)
        return 0

    def read(self):
        data = self.ser.readline()
        data = data.decode('utf-8').strip()
        print(data)
        self.ser.flush()
        return data
    
    def read_(self):
        data = self.ser.readline()
        data = data.decode('utf-8').strip()
        print(data)
        return data


if __name__ == '__main__':
    ser = uart('COM3')
    ser.send(20, 90)
    #print(ser.read_())
    print(ser.read())
    #print(ser.read_())
    